import {message as messages} from "antd";
import {apiDataType, typeObject} from 'types'

interface messageType {
    message?: string,
    type?: 'success' | 'info' | 'warning' | 'error',
    duration?: number
}

export const message: (props: messageType) => unknown = (props) => {
    const {message = '成功', type = 'success', duration = 2} = props
    messages[type](message, duration);
};

/*
*   清空对象
* */
export const clearObject = (object: { [propName: string]: any }) => {
    for (const k in object) {
        object[k] = ''
    }
    return object
}

/*
*   保留api接口需要的属性中的属性
* */
const initValue = ['page', 'pageSize']
export const removeObject = (goods: { [propName: string]: any }, value?: string[]) => {
    const values = value ? [...initValue, ...value] : initValue
    const data: { [k: string]: string | number } = {}
    values.forEach(item => {
        data[item] = goods[item]
    })
    return data as apiDataType
}

/*
* 判断是否有该属性
* */
export const isColumnsData: (data: typeObject<any>[], str: string) => boolean = (data, str) => data.some((column) =>
    column.dataIndex === str
)


/*
* 定时器
* */
export const timeOut: (callback: any, time?: number) => void = (callback, time = 0) => {
    setTimeout(() => {
        callback()
    }, time)
}


/*
* 默认页码属性
* */
export const initPageConfig = {
    page: 1,
    pageSize: 10,
    update: false
}

/*
* 获取对象中的key 返回包含key的数组
* */
const deleteValue = ['update', 'page', 'pageSize']
export const filterObject: (data: typeObject, value?: string[]) => string[] = (data, value = []) => {
    const list = Object.keys(data)
    value.push.apply(value, deleteValue)
    value.forEach(item => list.splice(list.indexOf(item), 1))
    return list
}

